#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: report.py 
@version:
@time: 2022/03/21 
"""
from datetime import datetime

from TapClientCore.T01_init_conf.path_conf import PathConf
from TapUtil.Tfeishu.feishuReport.report_base import ReportBase, read_json
from TapUtil.operatefile.path_operate import *


class ReportTestCase(ReportBase):
    template_path = path_joint(
        get_dirname(get_realpath(__file__)),
        'feishu_message_card.json')

    template = read_json(template_path)
    message = {}

    def __init__(self):
        data = read_json(PathConf.CURRENT_BUILD_NUMBER_PATH)
        self.web_hook_url = data.get('webHookUrl')
        self.project = data.get('project')[0]
        self.number = data.get('project')[1]
        self.params = data.get('params')
        self.start_time = data.get('start_time')
        self.url = data.get('url')
        self.version = data.get('version')
        self.now_time = datetime.now().strftime("%Y-%m-%d  %H:%M:%S")

    def template_edit(self):
        from pprint import pprint
        title = f"{self.project} {self.number} 执行结果"
        start_time = self.start_time
        end_time = self.now_time
        execute_time = self.time_dif(start_time, end_time)
        execute_analyse = self.analyse()
        color = execute_analyse.get('color')
        allure_report_url = f'{self.url}/allure'
        current_build_url = self.url
        current_build_attr = self.params

        payload = self.template.get('payload')
        text1 = self.template.get('text1')
        text2 = self.template.get('text2')
        text3 = self.template.get('text3')
        button_url1 = self.template.get('button_url1')
        button_url2 = self.template.get('button_url2')

        # message
        self.message = payload.copy()
        # title
        self.message['card']['header']['title']['content'] = title
        self.message['card']['header']['template'] = color
        # time
        _new_time = f"Tapdata version: {self.version}\n\n开始时间: {start_time}\n结束时间: {end_time}\n耗时:     {execute_time}"
        _text = text1.copy()
        _text['text']['content'] = str(_new_time)
        self.message['card']['elements'].append(_text)

        _case_result = f"共执行测试用例{execute_analyse.get('all_case')}个\n" \
                       f"成功测试用例{execute_analyse.get('pass_case')}个\n" \
                       f"失败测试用例{execute_analyse.get('fail_case')}个\n" \
                       f"跳过测试用例{execute_analyse.get('skip_case')}个\n" \
                       f"重跑测试用例{execute_analyse.get('rerun_case')}个\n"

        _text = text2.copy()
        _text['text']['content'] = str(_case_result)
        self.message['card']['elements'].append(_text)
        # f"共执行测试用例{self.all_case}个\n"
        # f"成功测试用例{self.pass_case}个\n"
        # f"失败测试用例{self.fail_case}个\n"
        # f"跳过测试用例{self.skip_case}个\n"
        # f"重跑测试用例{self.rerun_case}个\n"
        # allure
        _allure_title = f"{self.project} 本次执行报告"
        _button1 = button_url1
        _button1['actions'][0]['text']['content'] = str(_allure_title)
        _button1['actions'][0]['url'] = allure_report_url
        self.message['card']['elements'].append(_button1)

        # current build
        _build_title = f"{self.project} 本次构建"
        _button2 = button_url2
        _button2['actions'][0]['text']['content'] = str(_build_title)
        _button2['actions'][0]['url'] = current_build_url
        self.message['card']['elements'].append(_button2)

        # # 获取当前构建时写入执行参数信息
        tmp_str = ''
        for k, v in self.params.items():
            if not v:
                v = 'None'
            tmp_str += f"{k}:  {v}\n"
        _new_params = f"{tmp_str}"
        _text = text3.copy()
        _text['text']['content'] = str(_new_params)
        self.message['card']['elements'].append(_text)

    def report(self):
        self.send(self.web_hook_url, self.message)


if __name__ == '__main__':
    T = ReportTestCase()
    T.template_edit()
    T.report()
